'use strict'

const Controller = require('egg').Controller

class UserController extends Controller {
  async get() {
    this.ctx.body = await {
      status: 0,
      msg: 'ok',
      body: this.ctx.service.user.get(),
    }
  }

  async add() {
    this.ctx.body = await {
      status: 0,
      msg: 'ok',
      body: this.ctx.service.user.add(),
    }
  }

  async edit() {
    const result = await this.ctx.service.user.edit()
    this.ctx.body = {
      status: result.n === 1 ? 0 : 1,
      msg: result.n === 1 ? 'ok' : result,
      body: {},
    }
  }

  async delete() {
    const result = await this.ctx.service.user.delete()
    if (result.n === 1) {
      this.ctx.body = {
        status: 0,
        msg: 'ok',
        body: {},
      }
    } else {
      this.ctx.body = {
        status: 1,
        msg: result,
      }
    }
  }
}

module.exports = UserController
